package client.view;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;

import client.net.SimpleClient;

public class CmdView {
    private String ip;

    public void run() {
        boolean flag = true;
        
        File file = new File("ip.txt");
        if (file.exists()) {
            Scanner sc = new Scanner(System.in);
            System.out.print("是否使用之前的IP地址？(y/n): ");
            String choice = sc.nextLine().toLowerCase();
            if (choice.equals("y")) {
                try {
                    Scanner fileScanner = new Scanner(file);
                    ip = fileScanner.nextLine();
                    fileScanner.close();
                } catch (IOException e) {
                    System.out.println("读取IP地址失败！");
                    e.printStackTrace();
                }
            } else {
                inputNewIp();
            }
        } else {
            inputNewIp();
        }

        while (flag) {
            display();
            int n = input();
            flag = process(n);
        }
    }

    private void display() {
        System.out.println("学生成绩查询菜单：");
        System.out.println("1. Get Student Grade");
        System.out.println("2. Get Student Information");
        System.out.println("3. Add Student Information");
        System.out.println("0. Exit");
    }

    private int input() {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        return n;
    }

    private boolean process(int n) {
        boolean flag = true;
        if (n == 1) {
            String grade = getGrade();
            System.out.println("grade = " + grade);
        } else if (n == 2) {
            String studentInfo = getStudentInfo();
            String result = getInfo(studentInfo);
            System.out.println("Student Information: " + result);
        }  else if (n == 3) {
      inputNewStudentGrade();
      }
          else if (n == 0) {
            flag = false;
        } else {
            System.out.println("(0-2)!!");
        }
        return flag;
    }

    private String getGrade() {
        String code = "A01";
        String commandStr = getCommandCode(code);
        SimpleClient sc = new SimpleClient(ip, 4330, commandStr);
        String responseStr = sc.run();
        String result = getStudentGrade(responseStr);
        return result;
    }

    private String getStudentInfo() {
        String code = "A02";
        String commandStr = getCommandCode(code);
        SimpleClient sc = new SimpleClient(ip, 4330, commandStr);
        String responseStr = sc.run();
        return responseStr;
    }

   private void inputNewStudentGrade() {
    Scanner sc = new Scanner(System.in);
    System.out.println("Enter student information (Format: 学号,姓名,年龄,成绩):");
    String studentInfo = sc.nextLine();
    String response = sendNewStudentGrade(studentInfo);
    System.out.println("Server response: " + response);
  }

    private String getCommandCode(String code) {
        Scanner sc = new Scanner(System.in);
        System.out.println("Input student code:");
        String sid = sc.nextLine();
        String result = code + "-" + sid;
        return result;
    }

    private String getStudentGrade(String cmdStr) {
        String gradeStr = "None";
        String[] result = cmdStr.split("-");
        if (result.length != 2) {
            gradeStr = "None";
        } else {
            if ("A01".equals(result[0])) {
                gradeStr = result[1].trim();
            }
        }
        return gradeStr;
    }

    private String getInfo(String studentInfo) {
        String InfoStr = "None";
        String[] result = studentInfo.split(",");
        if (result.length != 4) {
            InfoStr = "None";
        } else {
            InfoStr = "学号:" + result[0].trim() + "  姓名:" + result[1].trim() + "  年龄:" + result[2].trim() + "  成绩:" + result[3].trim();
        }
        return InfoStr;
    }

   private String sendNewStudentGrade(String studentInfo) {
    String code = "A03";
    String commandStr =code+"-"+studentInfo;
    SimpleClient sc = new SimpleClient(ip,4330, commandStr);
    return sc.run();
}


      

    private void inputNewIp() {
        Scanner sc = new Scanner(System.in);
        System.out.print("请输入服务器IP地址：");
        ip = sc.nextLine();
        try {
            FileWriter writer = new FileWriter("ip.txt");
            writer.write(ip);
            writer.close();
        } catch (IOException e) {
            System.out.println("保存IP地址失败！");
            e.printStackTrace();
        }
    }
}
